package com.cloudcc.mobile.view.activity;

import java.text.SimpleDateFormat;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;

import com.cloudcc.cloudframe.net.ErrorInfo;
import com.cloudcc.cloudframe.net.ResultCallBack;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.dao.impl.CreateNewScheduleEngineImpl;
import com.cloudcc.mobile.dialog.DatetimePickDialog;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.view.base.BaseActivity;

/**
 * 日历管理主页之新建任务
 * 
 * @author lhy
 * @date 2015-11-24 上午9:44
 */
public class CreateNewTaskActivty extends BaseActivity implements
		OnClickListener {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	private CreateNewScheduleEngineImpl engine = new CreateNewScheduleEngineImpl();
	private static final int ADDMICROPOST_ACTIVITY_AT = 8002;
	private AlertDialog dialog;
	private LayoutInflater inflater = null;
	@SuppressLint("SimpleDateFormat")
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	@SuppressLint("SimpleDateFormat")
	SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	// private String week;
	int point = 0;
	private int heith = 0;
	@Bind(R.id.bar_back)
	TextView cancel;
	@Bind(R.id.bar_button_plus)
	TextView save;
	@Bind(R.id.bar_title_text)
	TextView title;
	@Bind(R.id.create_taskname)
	// 任务名称
	EditText taskname;
	@Bind(R.id.responsible_man_lay)
	// 责任人
	LinearLayout responsible_man_lay;
	@Bind(R.id.responsible_man)
	TextView responsible_man;
	@Bind(R.id.task_deadline_lay)
	// 截止时间
	LinearLayout deadline_lay;
	@Bind(R.id.task_deadline)
	TextView deadline;
	@Bind(R.id.task_warn_time_lay)
	// 提醒时间
	LinearLayout warn_time_lay;
	@Bind(R.id.task_warn_time)
	TextView warn_time;
	@Bind(R.id.add_task)
	// 添加新建事项
	LinearLayout add_task;
	@Bind(R.id.add_task_show)
	// 添加新建事项显示
	LinearLayout task_show;
	@Bind(R.id.task_show_detail)
	// 显示更多
	LinearLayout show_detail;
	@Bind(R.id.important_client_lay)
	// 重要客户
	LinearLayout client_lay;
	@Bind(R.id.important_client)
	TextView client;
	@Bind(R.id.task_participate_man_lay)
	// 参与人
	LinearLayout participate_manlay;
	@Bind(R.id.task_participate_man)
	TextView participate_man;
	@Bind(R.id.task_description)
	// 任务描述
	EditText description;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if ("en".equals(mEns)) {
			title.setText("Create a new task");
		}else{
			title.setText("新建任务");
	}
		
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		heith = size.y * 3 / 4;
		inflater = (LayoutInflater) this
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		add_task.setOnClickListener(this);
		deadline_lay.setOnClickListener(this);
		warn_time_lay.setOnClickListener(this);
		responsible_man_lay.setOnClickListener(this);
		participate_manlay.setOnClickListener(this);
		initdata();
	}

	public void initdata() {

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.create_task_layout;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bar_button_plus:
			showWaitDialog();
			JSONObject json = new JSONObject();
			engine.SaveNewTaskRequest(json, new ResultCallBack<Object>() {

				@Override
				public void onSuccess(Object t) {
					// TODO Auto-generated method stub
					if ("en".equals(mEns)) {
						Toast.makeText(CreateNewTaskActivty.this, "Save successfully, please continue！",
								Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(CreateNewTaskActivty.this, "保存成功，请继续操作！",
								Toast.LENGTH_SHORT).show();
				}
					
					CreateNewTaskActivty.this.finish();
				}

				@Override
				public void onFailure(ErrorInfo error) {
					// TODO Auto-generated method stub
					if ("en".equals(mEns)) {
						Toast.makeText(CreateNewTaskActivty.this, "Save failed, please try again！",
								Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(CreateNewTaskActivty.this, "保存失败，请重试！",
								Toast.LENGTH_SHORT).show();
				}
					
					dismissWainting();
				}
			});
		case R.id.add_task:
			add_task.setVisibility(View.GONE);
			task_show.setVisibility(View.VISIBLE);
			show_detail.setVisibility(View.VISIBLE);
			break;
		case R.id.task_deadline_lay:
			// dialog = new AlertDialog.Builder(CreateNewTaskActivty.this)
			// .setTitle("请选择日期时间").setView(getDateTimePick()).show();
			if ("en".equals(mEns)) {
				dialog = new DatetimePickDialog(CreateNewTaskActivty.this,
						"Please select the date and time", deadline, dialog, inflater).ShowDatetimeDialog();
			}else{
				dialog = new DatetimePickDialog(CreateNewTaskActivty.this,
						"请选择时间日期", deadline, dialog, inflater).ShowDatetimeDialog();
		}
			
			dialog.show();
			break;
		case R.id.task_warn_time_lay:
			final String descArr[] = getResources().getStringArray(
					R.array.array_time_warn);
			String s = "请选择提醒时间";
			if ("en".equals(mEns)) {
				s = "Please select the reminder time";
			}else{
				s = "请选择提醒时间";
		}
			dialog = new AlertDialog.Builder(CreateNewTaskActivty.this)
					.setTitle(s)
					.setSingleChoiceItems(descArr, point,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface arg0,
										int arg1) {
									// TODO Auto-generated method stub
									warn_time.setText(descArr[arg1]);
									point = arg1;
									dialog.dismiss();
								}
							}).show();
			dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, heith);
			break;
		case R.id.responsible_man_lay:
			Intent intent1 = new Intent(CreateNewTaskActivty.this,
					ChoiceContactActivity.class);
			startActivityForResult(intent1, ADDMICROPOST_ACTIVITY_AT);
			break;
		case R.id.task_participate_man_lay:
			Intent intent2 = new Intent(CreateNewTaskActivty.this,
					ChoiceContactActivity.class);
			startActivityForResult(intent2, ADDMICROPOST_ACTIVITY_AT + 1);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == ADDMICROPOST_ACTIVITY_AT && resultCode == RESULT_OK) {// @某人
			String a = data.getStringExtra("name");
			String aid = data.getStringExtra("id");
			responsible_man.setText(a);
			responsible_man.setTag(aid);
		} else if (requestCode == ADDMICROPOST_ACTIVITY_AT + 1
				&& resultCode == RESULT_OK) {
			String a = data.getStringExtra("name");
			String aid = data.getStringExtra("id");
			participate_man.setText(a);
			participate_man.setTag(aid);
		}
	}
}
